package com.xtaller.test.api.base;

import com.alibaba.fastjson.JSONObject;
import com.xtaller.test.tps.RedisHelper;
import io.swagger.annotations.Api;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

/**
 * @version V1.0
 * @author: Taller
 * @date: 2019-08-30
 * @Description:
 */
@Api(tags = "缓存api")
@RestController
@RequestMapping("/v1/redis")
public class RedisApi {
    @Autowired private RedisHelper redisHelper;

    @GetMapping("/{key}")
    @Cacheable(cacheNames = "a1")
    public Object get(@PathVariable String key){
        var val = redisHelper.get(key);
        return val.length() == 0 ? "找不到key的数据" : val;
    }

    @PostMapping()
    @CachePut(cacheNames="a1")
    public Object set(@RequestBody JSONObject object){
        var val = redisHelper.set(object.getString("key"), object.getString("val"));
        return val == true ? "缓存成功" : "缓存失败";
    }

}
